import 'package:flutter/material.dart';
import 'package:flutter_app/comics/ComicsTabPage.dart';
import 'package:flutter_app/common/Global.dart';
import 'package:flutter_app/login/MainBody.dart';
import 'package:flutter_app/novel/NovelPage.dart';
import 'MyDrawer.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    Global().init();
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.yellow,
        ),
        home: Home());
    // home: ChangeNotifierProvider(create: (_) => PageModel(), child: Home()));
  }
}

class Home extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return PageState();
  }
}

class PageState extends State<Home> {
  var pages = [
    MainBody(),
    NovelPage(),
    ComicsTabPage(),
    Text("动漫")
  ];
  var _currentPage = 2;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("这是一个Appbar"),
      ),
      // body: pages[context.watch<PageModel>().currentPage],
      body: pages[_currentPage],
      bottomNavigationBar: mainBottomNavigationBar(),
      drawer: MyDrawer(),
    );
  }

  ///底部导航
  BottomNavigationBar mainBottomNavigationBar() {
    return BottomNavigationBar(
      //超过3个Item时必须要这个type，否则一片白看不清
      type: BottomNavigationBarType.fixed,
      backgroundColor: Colors.yellowAccent,
      currentIndex: _currentPage,
      items: [
        BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页"),
        BottomNavigationBarItem(icon: Icon(Icons.book), label: "小说"),
        BottomNavigationBarItem(icon: Icon(Icons.image), label: "漫画"),
        BottomNavigationBarItem(icon: Icon(Icons.tv), label: "动漫"),
      ],
      onTap: (index) {
        // Provider.of<PageModel>(context,listen: false).changePage(index);
        if (_currentPage != index) {
          setState(() {
            _currentPage = index;
          });
        }
      },
    );
  }
}
